﻿using Microsoft.AspNetCore.Mvc;
using Nest;
using Spark.Config.Api.DTO.Log;
using Spark.Core.Values;
using Spark.Elasticsearch;
using System;
using System.Collections.Generic;
using System.Linq;
using Spark.Config.Api.AppCode;
using Spark.Config.Api.DTO.App;
using Spark.Config.Api.Services.Abstractions;

namespace Spark.Config.Api.Controllers
{
    public class LogController : BaseController
    {
        private readonly IElasticClientFactory _elasticClientFactory;
        private readonly IAppServices _appServices;

        public LogController(IElasticClientFactory elasticClientFactory
            , IAppServices appServices)
        {
            _elasticClientFactory = elasticClientFactory;
            _appServices = appServices;
        }

        /// <summary>
        /// 日志记录分页接口查询
        /// </summary>
        [HttpGet]
        public IActionResult List([FromServices] IPower power, [FromQuery]LogSearchRequest model)
        {
            SearchRequest searchRequest = new SearchRequest();
            BoolQuery boolQuery = new BoolQuery();

            var mustQuery = new List<QueryContainer>();
            var mustNotQuery = new List<QueryContainer>();
            var filterQuery = new List<QueryContainer>();
            var shouldQuery = new List<QueryContainer>();

            if (model.StartDate != null)
            {
                filterQuery.Add(
                    new DateRangeQuery
                    {
                        GreaterThanOrEqualTo = model.StartDate,
                        LessThanOrEqualTo = model.EndDate ?? DateTime.Now,
                        Field = "dateTime"
                    });
            }

            if (!string.IsNullOrWhiteSpace(model.RequestPath))
            {
                //请求地址
                mustQuery.Add(new MatchQuery { Field = "path", Query = model.RequestPath });
            }

            if (!string.IsNullOrWhiteSpace(model.UserName))
            {
                //账户
                mustQuery.Add(new MatchQuery { Field = "userName", Query = model.UserName });
            }

            if (model.LogType.HasValue)
            {
                //筛选业务日志以及系统日志
                if (model.LogType == 1)
                {
                    filterQuery.Add(
                        new TermRangeQuery
                        {
                            Field = "eventId.id",
                            GreaterThanOrEqualTo = "1000"
                        });
                }
                else if (model.LogType == 2)
                {
                    filterQuery.Add(
                        new TermRangeQuery
                        {
                            Field = "eventId.id",
                            LessThan = "1000"
                        });
                }
            }

            if (!string.IsNullOrWhiteSpace(model.Keywords))
            {
                mustQuery.Add(
                    new MultiMatchQuery
                    {
                        Fields = new[] { "message", "exceptionMessage" },
                        Query = model.Keywords
                    });
            }

            if (!string.IsNullOrWhiteSpace(model.Level))
            {
                //日志级别
                mustQuery.Add(new MatchQuery { Field = "level", Query = model.Level });
            }

            if (!string.IsNullOrWhiteSpace(model.AppCode))
            {
                shouldQuery.Add(
                    new TermQuery { Field = "appCode", Value = model.AppCode }
                );
            }
            else
            {
                if (power.IsAdmin != 1)
                {
                    var appList = _appServices.LoadList(
                        new AppSearchRequest
                        {
                            PageIndex = 1,
                            PageSize = 999
                        })
                        .List
                        .ToList();

                    if (appList.Count == 0)
                    {
                        return Json(default(QueryPageResponse<LogResponse>));
                    }
                }
            }

            mustNotQuery.Add(new MatchQuery { Field = "path", Query = "/status" });

            boolQuery.Must = mustQuery;
            boolQuery.MustNot = mustNotQuery;
            boolQuery.Filter = filterQuery;

            if (shouldQuery.Count > 0)
            {
                boolQuery.Should = shouldQuery;
                boolQuery.MinimumShouldMatch = 1;
            }

            searchRequest.Sort = new List<ISort> { new SortField { Field = "dateTime", Order = SortOrder.Descending } };
            searchRequest.From = model.PageIndex == 0 ? 0 : (model.PageIndex - 1) * model.PageSize;
            searchRequest.Size = model.PageSize;
            searchRequest.Query = boolQuery;

            var logList = _elasticClientFactory.Client.Search<LogResponse>(searchRequest);
            return Json(
                new QueryPageResponse<LogResponse>
                {
                    List = logList.Documents,
                    Total = logList.Total
                });
        }

        /// <summary>
        /// 创建索引
        /// </summary>
        [HttpPost]
        public IActionResult Index(LogIndexRequest request)
        {
            return Json();
        }
    }
}